/**
 * 初始化
 */
var ExcelGoods = {
    excelid: "ExcelTable",
    switchs: {},
    tableName: "",
    submitData: {},
    excelval: {},
    table: [],
    exceltable: [],
    form: null,
    sortEnglishName: {}
};

ExcelGoods.initColumn = function (changeDate) {

    var goodId = {title: '商品id', field: 'goodId', visible: true, align: 'center', valign: 'middle'};
    var goodName = {title: '商品名称', field: 'goodName', visible: true, align: 'center', valign: 'middle'};
    var categoryType = {title: '商品分类', field: 'categoryType', visible: true, align: 'center', valign: 'middle'};
    var goodDescription = {title: '商品描述', field: 'goodDescription', visible: true, align: 'center', valign: 'middle', sortable: false};
    var goodIcon = {title: '商品图片', field: 'goodIcon', visible: true, align: 'center', valign: 'middle'};
    var goodInventory = {title: '商品库存', field: 'goodInventory', visible: true, align: 'center', valign: 'middle', sortable: true};
    var goodPrice = {title: '商品价格', field: 'goodPrice', visible: true, align: 'center', valign: 'middle', sortable: true};
    var productStatus = {title: '商品状态', field: 'productStatus', visible: true, align: 'center', valign: 'middle'};

    var data = [{field: 'selectItem', radio: true, visible: false}];
    for (var item in ExcelGoods.sortEnglishName) {
        if (item == "goodId" && ExcelGoods.sortEnglishName[item] == true) {
            data.push(goodId)
        }
        if (item == "goodName" && ExcelGoods.sortEnglishName[item] == true) {
            data.push(goodName)
        }
        if (item == "categoryType" && ExcelGoods.sortEnglishName[item] == true) {
            data.push(categoryType)
        }
        if (item == "goodDescription" && ExcelGoods.sortEnglishName[item] == true) {
            data.push(goodDescription)
        }
        if (item == "goodIcon" && ExcelGoods.sortEnglishName[item] == true) {
            data.push(goodIcon)
        }
        if (item == "goodInventory" && ExcelGoods.sortEnglishName[item] == true) {
            data.push(goodInventory)
        }
        if (item == "goodPrice" && ExcelGoods.sortEnglishName[item] == true) {
            data.push(goodPrice)
        }
        if (item == "productStatus" && ExcelGoods.sortEnglishName[item] == true) {
            data.push(productStatus)
        }

    }
    return data;
};
/**
 * 选择导出excel列
 */
ExcelGoods.selectExcelTemplate = function (templateKey) {

    if (SelectList.mutiSelect("templateList", "key", templateKey) == true) {
        ExcelGoods.switchs[templateKey] = true;

        ExcelGoods.excelval[templateKey] = $("#" + templateKey).val();

    } else {
        ExcelGoods.switchs[templateKey] = false;
    }

};
/**
 * 选择全部
 */
ExcelGoods.chooseAllExcel = function () {

    $("#templateList").find("a").each(function (i, d) {
        $(this).addClass('active');
    });

    if ($('#allListChoose').find("a").length != 0) {
        $('#allListChoose').find("a").remove();
    }

    ExcelGoods.switchs = {
        goodId: true,
        categoryType: true,
        goodDescription: true,
        goodIcon: true,
        goodInventory: true,
        goodName: true,
        goodPrice: true,
        productStatus: true
    };

    var ajax = new $ax(Feng.ctxPath + "/goods/excelTableName", function (data) {
        for (var j in data) {
            if (j == "selectname") {
                $.each(data[j], function (value) {
                    $('#allListChoose').append('<a href="javascript:void(0)" class="list-group-item" >' + data[j][value] + '</a>');

                })
                for (var item in ExcelGoods.switchs) {
                    ExcelGoods.sortEnglishName[item] = ExcelGoods.switchs[item];
                }
            }
        }
        //
    });
    ajax.setData(ExcelGoods.switchs);
    ajax.start();

};
/**
 * 清空数据
 */
ExcelGoods.clean = function () {
    $("#templateList").find("a").each(function (i, d) {
        if ($(this).hasClass('active')) {
            $(this).removeClass('active');
        }
    });
    if ($('#allListChoose').find("a").length != 0) {
        $('#allListChoose').find("a").remove();
    }

    ExcelGoods.switchs = {};
    ExcelGoods.sortEnglishName={};
}
/**
 * 选择
 */
ExcelGoods.chooseExcel = function () {


    if ($('#allListChoose').find("a").length != 0) {
        $('#allListChoose').find("a").remove();
    }
    ExcelGoods.submitData = {};

    for (var item in ExcelGoods.switchs) {
        ExcelGoods.submitData[item] = ExcelGoods.switchs[item];
    }

    var ajax = new $ax(Feng.ctxPath + "/goods/excelTableName", function (data) {
        for (var j in data) {
            if (j == "selectname") {
                ExcelGoods.exceltable = data[j];
                $.each(data[j], function (value) {
                    $('#allListChoose').append('<a href="javascript:void(0)" class="list-group-item" >' + data[j][value] + '</a>');
                })
            } else {

                for(var i=0;i<data[j].length;i++){
                    ExcelGoods.sortEnglishName[data[j][i]] = true;
                }
            }
        }
        //
    });
    ajax.setData(ExcelGoods.submitData);
    ajax.start();

};
/**
 * 打印Excel
 */
ExcelGoods.getExcel = function () {
    var postRequest = function (reqUrl, params, callback) {
        if ($("#postForm").length > 0) {
            $("#postForm").remove();
        }
        var dyform = $("<form id='postForm'></form>");
        dyform.attr('action', reqUrl);
        dyform.attr('method', 'post');
        for (var key in params) {
            var dyinput = $("<input type='hidden' name='" + key + "' />");
            dyinput.attr("value", params[key]);
            dyform.append(dyinput);
        }
        dyform.css('display', 'none');
        dyform.appendTo("body");
        dyform.submit();
        if ($.isFunction(callback)) {
            callback();
        }
    };

    if ($.isEmptyObject(ExcelGoods.sortEnglishName)) {
        Feng.error("请选择数据！！")
    } else {
        postRequest(Feng.ctxPath + "/goods/getExcel", ExcelGoods.sortEnglishName, {"table": "<table></table>"});
    }


};


/**
 * 浏览
 */
ExcelGoods.skin = function () {


    if ($('#excel').find("table").length != 0) {
        $('#excel').find("table").remove();
        $('#excel').find("div").remove();
    }

    $('#excel').append("<table id='ExcelTable'/>");

    ExcelGoods.submitData = {};

    var defaultColunms = ExcelGoods.initColumn();
    var table = new BSTable(ExcelGoods.excelid, "/goods/getskin", defaultColunms);
    table.setPaginationType("client");
    for (var item in ExcelGoods.sortEnglishName) {
        ExcelGoods.submitData[item] = ExcelGoods.sortEnglishName[item];
    }
    $.extend(table.queryParams, ExcelGoods.submitData);

    ExcelGoods.form = table.init();
    ExcelGoods.form.refresh();

};

$(function () {

});